/**
 * Created by kevin on 2017/01/08.
 *
 * 背景音乐
 * 音乐icon
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function Music(config) {

        this.Container_constructor();
        this.config = config;
        this.init();

    }


    var p = createjs.extend(Music, createjs.Container);
    p.className = "Music";
    //加载器
    p.loader;
    //图标
    p.musicIcon;
    //配置
    p.configMusic;
    //开、关
    p.isOns = true;
    p.config;


    p.init = function () {
        this.loadRes();
        this.isOns = !BGM.play_object.paused;
    }


    /*
     * 加载资源
     * */
    p.loadRes = function(){
        this.loader = new createjs.LoadQueue(true);
        this.loader.on("complete", this.loadComplete, this);
        this.loader.on("fileload", this.onLoaded, this);
        this.loader.loadManifest({src: this.config.src, callback: "loadMusicMediaGrid", type: "manifest"}, true, "");

    }
    /*
    * 资源加载
    * */
    p.onLoaded = function (evt){
        //console.log(evt)
        var item = evt.item;
        switch (item.id){
            case "uilayer_music_icon":
                this.configMusic = evt.item;
                break;
            case "uilayer_bgmusic":
                this.configBgMusic = evt.item;
                break;
        }

    }
    /*
     * 资源加载完毕
     * */
    p.loadComplete = function (evt){
        this.isOns = !BGM.play_object.paused;
        if(this.configMusic){
            this.drawMusic();
        }
    }

    /*
     * 绘制音乐按钮
     * */
    p.drawMusic = function(){
        var hasMusicPic = this.loader.getResult("uilayer_music_icon");
        if(hasMusicPic){
            var icon_scale = targetWidth*this.configMusic.zoom/hasMusicPic.width;
            this.musicIcon = new eosi.website.BaseButton();

            var graphics = new createjs.Graphics().beginFill("#000000").drawCircle(0, 0, icon_scale*hasMusicPic.width/2);
            var iconBg = new createjs.Shape(graphics);
            graphics.alpha = .01;

            var iconPic = new createjs.Bitmap(hasMusicPic);
            //console.log(this.config.fit)

            iconPic.scaleX = iconPic.scaleY = icon_scale;
            //iconPic.regX = -hasMusicPic.width * icon_scale*.5;
            //iconPic.regY = -hasMusicPic.height * icon_scale*.5;
            iconPic.x = -hasMusicPic.width * icon_scale*.5;
            iconPic.y = -hasMusicPic.height * icon_scale*.5;
            this.musicIcon.addChild(iconBg,iconPic);
            this.addChild(this.musicIcon);
            //return;
            var offset_x = targetWidth * 0.02 + hasMusicPic.width * iconPic.scaleX*.5;
            var offset_y = offset_x;
            switch (this.configMusic.align){
                case "TL"://左上
                    this.musicIcon.x = offset_x;
                    this.musicIcon.y = offset_y;
                    break;
                case "TR"://右上
                    this.musicIcon.x = targetWidth - offset_x;
                    this.musicIcon.y = offset_y;
                    break;

                case "BL"://左下
                    this.musicIcon.x = offset_x;
                    this.musicIcon.y = targetHeight - offset_y;
                    break;
                case "BR"://右下
                    this.musicIcon.x = targetWidth - offset_x;
                    this.musicIcon.y = targetHeight - offset_y;
                    break;

                default :
                    this.musicIcon.x = targetWidth - offset_x;
                    this.musicIcon.y = offset_y;
                    break;

            }
        }


        this.musicIcon.on("mousedown", this.onClick, this);

    }

    p.onClick = function (evt) {
        if (!BGM.play_object.paused) {
            BGM.pause();
            this.isOns=false;
        }else{
            BGM.play();
            this.isOns=true;
        }
    }

    p.tick = function(evt){
        if(this.isOns){
            if(this.musicIcon){
                this.musicIcon.rotation++;
            }

        }
        // if (!BGM.play_object.paused) {
        //     if(this.musicIcon){
        //         this.musicIcon.rotation++;
        //     }
        // }
    }




    // add MyApp to myNameSpace
    window.eosi.website.Music = createjs.promote(Music, "Container");
//}());
}(window));





